package com.riatimes.util
{
	import com.carlcalderon.arthropod.Debug;
	import com.riatimes.event.LoggerEvent;

	/**
	 * 日志类
	 * @author Nick Wong
	 */
	public class Logger
	{
		/**
		 *
		 * 是否打印日志
		 */
		public static var enableDebug:Boolean=true;

		private static function log(s:String):void
		{
			trace(s);
		}

		/**
		 * 打印信息
		 * @param args
		 */
		public static function logInfo(... args):void
		{
			var s:String="[" + LoggerEvent.INFO.toUpperCase() + "] " + args.join(" ");
			log(s);
			if (enableDebug)
				Debug.log(s);
		}

		/**
		 *
		 * @param msg
		 */
		public static function debugTrace(msg:String):void
		{
			//trace( "--> " + msg );
			Logger.logInfo("--> " + msg);
			//ModelLocator.testArea.appendText("--> " + msg+"\n");
		}

		/**
		 * 遍历对象
		 * @param obj
		 * @param depth
		 */
		public static function loopObj(obj:Object, depth:int=0):void
		{
			for (var item:* in obj)
			{
				if (obj[item])
				{
					debugTrace(getBlank(depth) + item + ":" + obj[item]);
					loopObj(obj[item], (depth + 1));
				}
				else
				{
					debugTrace(getBlank(depth) + item + ":" + obj[item]);
				}
			}
		}

		/**
		 * 获取空格
		 * @param deep
		 * @return
		 */
		public static function getBlank(deep:int=0):String
		{
			var s:String="";
			for (var i:uint=0; i < deep; i++)
			{
				s+="   ";
			}
			return s;
		}

		/**
		 * 打印对象
		 * @param args
		 */
		public static function logObject(... args):void
		{
			if (enableDebug)
			{
				if (args.length > 1)
					log(args[1]);
				//Debug.object(args[0]);
				loopObj(args[0]);
			}
		}

		/**
		 * 打印数组
		 * @param args
		 */
		public static function logArray(... args):void
		{
			if (enableDebug)
			{
				if (args.length > 1)
					log(args[1]);
				Debug.array(args[0]);
			}
		}

		/**
		 * 打印位图
		 * @param bmd
		 */
		public static function logBitmap(bmd:*):void
		{
			Debug.bitmap(bmd);
		}

		/**
		 * 打印警告信息
		 * @param args
		 */
		public static function logWarning(... args):void
		{
			var s:String="[" + LoggerEvent.WARNING.toUpperCase() + "] " + args.join(" ");
			log(s);
			if (enableDebug)
				Debug.warning(s);
		}

		/**
		 * 打印错误信息
		 * @param args
		 */
		public static function logError(... args):void
		{
			var s:String="[" + LoggerEvent.ERROR.toUpperCase() + "] " + args.join(" ");
			log(s);
			if (enableDebug)
				Debug.error(s);
		}

		/**
		 * 清除输出日志
		 */
		public static function clear():void
		{
			if (enableDebug)
				Debug.clear();
		}
	}

}